<template>
  <div class="device-type-list-container">
    <header-search-form
      :searchFormData="searchFormData"
      @search="search"
    />
    <lan-table
      size="small"
      :config="dataTable"
      :tableData="tableData"
      @search="search"
    >
      <template #type="scope">
        <el-input v-model="scope.row.type"></el-input>
      </template>
    </lan-table>
    <new-edit
      ref="editComponent"
      @search="search"
    />

    <el-dialog
      width="600px"
      title="设备参数列表"
      :visible.sync="dialogTableVisible"
    >
      <el-table
        stripe
        :data="showReceiveDeviceTableData"
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="name"
          label="名称"
        ></el-table-column>
        <el-table-column
          prop="default"
          label="是否为默认参数"
          align="center"
        ><template slot-scope="scope">
            <div
              v-if="scope.row.default"
              style="color: rgba(102, 177, 255, 1);"
            >是</div>
            <div
              v-else
              style="color: rgba(144, 147, 153, 1);"
            >否</div>
          </template>
        </el-table-column>
      </el-table>
      <div class="receive-device-footer">
        <el-pagination
          background="#fff"
          @size-change="pageSizeChangeHandler"
          @current-change="currentPageChangeHandler"
          :current-page.sync="searchFormDialog.currentPage"
          :page-size.sync="searchFormDialog.pageSize"
          :page-sizes="[5, 10, 20, 50, 100]"
          layout="total,sizes, prev, pager, next, jumper"
          :total="searchFormDialog.totalCount"
          align="right"
        ></el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import lanTable from '../../../components/lanTable';
import newEdit from './newEdit';
import HeaderSearchForm from './headerSearchForm.vue';
import moment from 'moment';
import { MomentFormat, SortType } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { getDeviceTypeListApi, deleteDeviceTypeApi } from '../../../api/deviceType';
import { parseError } from '../../../utils';
import { getActiveSelector, getActiveDisplay, getActiveColor } from './service';
export default {
  components: {
    lanTable,
    newEdit,
    HeaderSearchForm
  },
  data() {
    return {
      dialogTableVisible: false,
      searchFormData: {
        btnLoading: false,
        name: '',
        active: ''
      },
      tableData: [],
      deviceParameterList: [],
      dataTable: {
        pageSize: 10,
        totalCount: 0,
        currentPage: 1,
        pageSizes: [5, 10, 20, 50, 100],
        sort: {
          orderBy: 'createdAt',
          sortType: SortType.DESC
        },
        columnList: [
          {
            type: 'index'
          },
          {
            name: 'name',
            label: '名称'
          },
          {
            name: 'description',
            label: '描述'
          },
          {
            name: 'imageUrl',
            label: '图片',
            type: 'image'
          },
          {
            label: '是否可用',
            type: 'formatter',
            name: 'active',
            scope: true,
            width: '100px',
            formatter: getActiveDisplay,
            style: {
              color: getActiveColor
            }
          },
          {
            label: '设备参数列表',
            type: 'button',
            name: 'deviceParameterList',
            scope: true,
            width: '100px',
            buttons: [
              {
                text: '查看',
                icon: 'el-icon-tickets',
                method: row => this.showDeviceParameterList(row)
              }
            ]
          },
          {
            name: 'createdAt',
            label: '创建时间',
            sortable: 'custom'
          },
          {
            name: 'updateAt',
            label: '更新时间'
          },
          {
            type: 'button',
            label: '操作',
            buttons: [
              {
                type: 'text',
                text: '编辑',
                size: 'small',
                method: row => {
                  this.showEdit(row);
                }
              },
              {
                type: 'text',
                text: '删除',
                size: 'small',
                method: row => {
                  this.remove(row.uuid, row.name);
                }
              }
            ]
          }
        ]
      },
      searchFormDialog: {
        currentPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      showReceiveDeviceTableData: []
    };
  },
  watch: {
    deviceParameterList: {
      handler(val) {
        this.searchFormDialog.totalCount = val.length;
        this.search();
      }
    }
  },
  created() {
    this.search();
  },
  methods: {
    getActiveDisplay,
    getActiveColor,
    searchDialog() {
      this.showReceiveDeviceTableData = this.deviceParameterList.slice(
        (this.searchFormDialog.currentPage - 1) * this.searchFormDialog.pageSize,
        this.searchFormDialog.currentPage * this.searchFormDialog.pageSize
      );
    },
    pageSizeChangeHandler() {
      this.searchFormDialog.currentPage = 1;
      this.searchDialog();
    },
    currentPageChangeHandler() {
      this.searchDialog();
    },
    search() {
      this.searchFormData.btnLoading = true;
      getDeviceTypeListApi(
        this.dataTable.currentPage - 1,
        this.dataTable.pageSize,
        this.dataTable.sort.sortType,
        this.dataTable.sort.orderBy,
        this.searchFormData.name,
        this.searchFormData.active
      )
        .then(({ data, total_count }) => {
          this.dataTable.totalCount = total_count;
          this.tableData = data.map(TypeList => {
            return {
              uuid: TypeList.uuid,
              name: TypeList.name,
              description: TypeList.description,
              imageUrl: TypeList.image_url,
              active: TypeList.active,
              deviceParameterList: TypeList.device_parameter_list,
              parameterIdList: TypeList.device_parameter_list.map(parameterList => parameterList.id),
              parameterNameList: TypeList.device_parameter_list.map(parameterList => parameterList.name),
              createdAt: moment(TypeList.created_at).format(MomentFormat),
              updateAt: moment(TypeList.update_at).format(MomentFormat)
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchFormData.btnLoading = false));
    },
    showEdit(row) {
      this.$refs.editComponent.showEditDialog(row);
    },
    remove(uuid, name) {
      MessageBox.confirm(`确定删除设备类型${name}吗？`, '删除提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          deleteDeviceTypeApi(uuid)
            .then(() => Message.success('删除成功'))
            .catch(error => Message.error(parseError(error)))
            .finally(() => this.search());
        })
        .catch(() => {});
    },
    showDeviceParameterList(data) {
      this.deviceParameterList = data.deviceParameterList;
      this.searchDialog();
      this.$nextTick(() => {
        this.searchFormDialog.totalCount = this.deviceParameterList.length;
        this.searchDialog();
      });
      this.dialogTableVisible = true;
    }
  }
};
</script>
<style lang="scss" scoped>
.device-type-list-container {
  padding: 10px;
}
.receive-device-footer {
  padding-top: 10px;
}
</style>
